<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.util.List" %>
<%@page import="java.util.ArrayList"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.io.InputStream" %>
<%@page import="java.util.regex.Matcher"%>
<%@page import="java.util.regex.Pattern"%>

<%@ page import = "java.sql.PreparedStatement" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Edit Curriculum</title>
	<link rel="stylesheet" type="text/css" href="css/stylev2.css" />
	<link rel="stylesheet" href="css/validation.css" />
	<link rel="stylesheet" href="css/jquery.mobile-1.3.2.min.css"/>
	<link rel="stylesheet" type="text/css" href="css/table-no-header.css" />
	<script type="text/javascript" src="js/livevalidation_standalone.js"></script> 
	<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
	
	<style type="text/css">
		a {
	      text-decoration:none;
	   }
	</style>
</head>
<body>
<%
//Get session
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
			
	if (htp_session == null || htp_session.getAttribute("sUser") == null) {
	    // Forward the control to login.jsp if authentication fails or session expires
	    request.getRequestDispatcher("/login.jsp").forward(request,
	        response);
	}
	
	if("admin".equals(htp_session.getAttribute("sUser"))){%>
	<script>alert("This user dont have PERMISSION to access this zone.");</script>
	<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=admin_home.jsp"> 	
	<%}
	
					int DepartmentIdOLD = Integer.parseInt((request.getParameter("departmentId")));
					String departmentOld = new String(request.getParameter("departmentName").getBytes("ISO8859_1"),"UTF-8");
					
					String DegreeOLD = new String(request.getParameter("Degree").getBytes("ISO8859_1"),"UTF-8");				
					System.out.println("EDIT DEGREE: " +DegreeOLD);//////////////////////////////////////////////////////	
					
					String ProgramOLD = new String(request.getParameter("Program").getBytes("ISO8859_1"),"UTF-8");	
					System.out.println("EDIT PROGRAM: " +ProgramOLD);//////////////////////////////////////////////////////
%>
<%	
//GET PERMISSION

	String name = String.valueOf(session.getAttribute("name"));
	String surname = String.valueOf(session.getAttribute("surname"));
	String role_name = String.valueOf(session.getAttribute("curriculum_role"));
	
	String department_id = String.valueOf(session.getAttribute("department_id"));
			
	System.out.println("name : " + name);///////////////////////////////
	System.out.println("department_id : " + department_id);/////////////////////////
	
	List<String> system_name = new ArrayList<String>();
	
	Permission permission = new Permission(strUser,name);
	permission.setDepartmentId(department_id);
	permission.setCurriculumCreate(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_create"))));
	permission.setCurriculumDelete(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_delete"))));
	permission.setCurriculumRead(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_read"))));
	permission.setCurriculumUpdate(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_update"))));
	
	String permiss = "";
	if(permission.getCurriculumCreate()) {
		permiss = permiss + "Add";
	}
	if(permission.getCurriculumUpdate()) {
		if("".equals(permiss)) {
			permiss = permiss + "Edit";
		} else {
			permiss = permiss + " Edit";
		}
	}
	if(permission.getCurriculumRead()) {
		if("".equals(permiss)) {
			permiss = permiss + "Read";
		} else {
			permiss = permiss + " Read";
		}
	}
	if(permission.getCurriculumDelete()) {
		if("".equals(permiss)) {
			permiss = permiss + "Delete";
		} else {
			permiss = permiss + " Delete";
		}
	}
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////			
//check permission
	if(!permission.getCurriculumUpdate()){%>
		<script>alert("This user dont have PERMISSION to access this zone.");</script>
		<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=curriculum_home.jsp"> 	
	<%}

//set Database Connection
String hostProps = "";
String usernameProps  = "";
String passwordProps  = "";
String databaseProps = "";

try {
	//get current path
	ServletContext servletContext = request.getSession().getServletContext();
	
	InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
	Properties props = new Properties();
	
	props.load(input);

	hostProps  = props.getProperty("host");
	usernameProps  = props.getProperty("username");
	passwordProps  = props.getProperty("password");
	databaseProps = props.getProperty("database");
} catch (Exception e) { 
	out.println(e);  
}

System.out.println("hostProperties : "+hostProps);////////////////try/////////////////
System.out.println("usernameProperties : "+usernameProps);////////////////try/////////////////
System.out.println("passwordProperties : "+passwordProps);////////////////try/////////////////
System.out.println("databaseProperties : "+databaseProps);////////////////try/////////////////

// connect database
		Connection connect = null;		
		try {
			Class.forName("com.mysql.jdbc.Driver");
		
			connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/" + databaseProps +
					"?user=" + usernameProps  + "&password=" + passwordProps + "&characterEncoding=tis620");//////"&characterEncoding=tis620" Encoding Thai);
		
			if(connect != null){
				System.out.println("Database Connect Sucesses.");
			} else {
				System.out.println("Database Connect Failed.");	
			}
		
		} catch (Exception e) {
			out.println(e.getMessage());
			e.printStackTrace();
		}	
		
		/* find department in database */
		List<Department> department = new ArrayList<Department>();
		
		try {			   
			ResultSet rs_department;
			if(("0").equals(department_id)){//all department
				rs_department  = connect.createStatement().executeQuery("SELECT department_id, department_name_th" 
					+ " FROM department " + " WHERE department_id != 0 ORDER BY department_id");
			}else{
				
				rs_department = connect.createStatement().executeQuery("SELECT department_id, department_name_th " 
						+ " FROM department " 
					    +" WHERE department_id="+permission.getDepartment_id()
					    );
			}
			
			while(rs_department.next()) {
				Department dep2 = new Department();
				
				dep2.setDepartmentNameTH(rs_department.getString("department_name_th"));
				dep2.setDepartmentId(Integer.parseInt(rs_department.getString("department_id")));
				department.add(dep2);
				
				department.size();
				
				System.out.println("department.size(): "+department.size());
			}
		
	} catch (SQLException e) {
		e.printStackTrace();
	}
%>	
<%		

	//get previous information form DB
				String CurriculumId = request.getParameter("CurriculumId");
				String CurriculumNameTHOLD = new String(request.getParameter("nameCurriculumTH").getBytes("ISO8859_1"),"UTF-8");
				String CurriculumNameENOLD = request.getParameter("nameCurriculumEN");
				String BachelorNameTHOLD = new String(request.getParameter("nameDegreeTH").getBytes("ISO8859_1"),"UTF-8");
				String BachelorNameENOLD = request.getParameter("nameDegreeEN");
				String ModifiedOLD = request.getParameter("modified");
				String CreditOLD = (request.getParameter("credit"));				
				
				System.out.println("EDIT : " +DepartmentIdOLD);//////////////////////////////////////////////////////*/
				System.out.println("EDIT : " +departmentOld);//////////////////////////////////////////////////////*/
				
				String YearOLD = request.getParameter("year");
				String CoorganizerOLD = request.getParameter("Coorganizer");
				String OHCEOLD = request.getParameter("OHCE");
				String ONECOLD = request.getParameter("ONEC");
				String EngAppOLD = request.getParameter("EngApp");	

				/* find Curriculum in database */
				List<Curriculum> curriculum = new ArrayList<Curriculum>();
				
				try {			   
					ResultSet rs_curriculum = connect.createStatement().executeQuery("SELECT `curriculum_id`, `curriculum_name_th`, `curriculum_name_en`, `bachelor_name_th`, `bachelor_name_en`, `modified`, `department_id`, `credit`, `year`, `co_organizer`, `degree`, `program`, `ohce_approved`, `onec_approved`, `curriculum_EngApp` " 
							+ " FROM curriculum " + " ORDER BY curriculum_id");
					
					while(rs_curriculum.next()) {
						Curriculum cur = new Curriculum();
						
						cur.setCurriculumNameTH(rs_curriculum.getString("curriculum_name_th"));
						cur.setCurriculumNameEN(rs_curriculum.getString("curriculum_name_en"));
						cur.setCurriculumId(Integer.parseInt(rs_curriculum.getString("curriculum_id")));
						cur.setBachelorNameTH(rs_curriculum.getString("bachelor_name_th"));
						cur.setBachelorNameEN(rs_curriculum.getString("bachelor_name_en"));
						cur.setModified(rs_curriculum.getString("modified"));
						cur.setCredit(Integer.parseInt(rs_curriculum.getString("credit")));
						cur.setDepartmentId(Integer.parseInt(rs_curriculum.getString("department_id")));
						cur.setYear(rs_curriculum.getString("year"));
						cur.setCoorganizer(rs_curriculum.getString("co_organizer"));
						cur.setDegree(rs_curriculum.getString("degree"));
						cur.setProgram(rs_curriculum.getString("program"));
						cur.setOHCE(rs_curriculum.getString("ohce_approved"));
						cur.setONEC(rs_curriculum.getString("onec_approved"));
						cur.setEngApp(rs_curriculum.getString("curriculum_EngApp"));
						
						curriculum.add(cur);
					}
					
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
				
				
				/*Find Course in DB*/
				List<Course> course3 = new ArrayList<Course>();
				try{
					ResultSet rs_cour3 = connect.createStatement().executeQuery("SELECT `course_id`,G.curriculum_id, C.group_id,C.curriculum_id,`course_name_th`, `course_name_en`, C.credit, `lecture_hour`,`lab_hour`, `self_learning_hour`, `detail`, G.ohce_approved, G.onec_approved, G.curriculum_EngApp"            
							+ " FROM course C, curriculum G "
							+ " WHERE G.curriculum_id = C.curriculum_id AND G.curriculum_id ="+CurriculumId+" "
							+ " ORDER BY `course_id`");
					
					while(rs_cour3.next()) {
						Course cour3 = new Course();
						
						cour3.setCourseId(rs_cour3.getString("C.course_id"));
						cour3.setGroupId(rs_cour3.getInt("C.group_id"));
						cour3.setCurriculumId(rs_cour3.getInt("C.curriculum_id"));
						cour3.setCourseNameTH(rs_cour3.getString("course_name_th"));
						cour3.setCourseNameEN(rs_cour3.getString("course_name_en"));
						cour3.setCredit(rs_cour3.getInt("C.credit"));		
						cour3.setLecture(rs_cour3.getInt("lecture_hour"));
						cour3.setLab(rs_cour3.getInt("lab_hour"));
						cour3.setSelfLearn(rs_cour3.getInt("self_learning_hour"));
						cour3.setDetails(rs_cour3.getString("detail"));
						cour3.setOHCE(rs_cour3.getString("ohce_approved"));
						cour3.setONEC(rs_cour3.getString("onec_approved"));
						cour3.setcurriculum_EngApp(rs_cour3.getString("curriculum_EngApp"));
						
						course3.add(cour3);						
					}
					
				}catch (SQLException e) {
					e.printStackTrace();
				}
				
				
%>
	<div id="page-wrap">
		<div id="inside">
			<table border="0">
				<tr style="font-size:24px">
					<td width="600" height="100" style= "background-color: #273b80;border: 0px ;padding: 0px; 0px;" >
						<div id="header_banner-2"></div>
					</td>
					<td width="680" height="100" style= "background-color: #273b80;border: 0px;padding: 0px; 0px;" >
						<p align="right" ><label><font size="4" color="#FFFFFF">  username : <%=strUser %>&nbsp;&nbsp;</font></label></p>
						<a href="logout.jsp"><p align="right"><label style="cursor: pointer;"><font size="3" color="#ffffff">log out &nbsp;</font></label></p></a>
					</td>							
				</tr>
			</table>
				<div id="main-content-2">
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<label><font size="5">แก้ไขข้อมูลหลักสูตร</font></label>
				</div>
				
					<!-- 2nd part-->
				<div id="main-content-ac" align="center" style="display: visible">	
					<table border="0">												
					<tr style="font-size:24px">
					<form id="form" name="form" method="post" action="">
						<input type="hidden" id="CurriculumId" name="CurriculumId" value="<%=CurriculumId%>" /> 
						
						
							<td width="500" height="50">
								<label for="labelnameCurriculumTH"><font size="5"><strong>ชื่อหลักสูตร (ภาษาไทย)</strong></font></label>
								<label><font size="5" color="#FF0000">*</font></label>
							</td>
							<td width="500" height="50">
								<input type="text" name="nameCurriculumTH" id="nameCurriculumTH" required value="<%=CurriculumNameTHOLD%>" />
								
							</td>
							<input type="hidden" id="CurriculumNameTH" name="CurriculumNameTH" value="<%=CurriculumNameTHOLD%>" /> 
						</tr>
						
						<tr style="font-size:24px">
							<td width="500" height="50">
								<label for="labelnameCurriculumEN"><font size="5"><strong>ชื่อหลักสูตร (ภาษาอังกฤษ)</strong></font></label>
								<label><font size="5" color="#FF0000">*</font></label>
							</td>
							<td width="500" height="50">
								<input name="nameCurriculumEN" type="text" style="font-size:18px" required value="<%=CurriculumNameENOLD%>" />
								
							</td>
							<input type="hidden" id="CurriculumNameEN" name="CurriculumNameEN" value="<%=CurriculumNameENOLD%>" /> 
						</tr>
						
						<tr style="font-size:24px">
							<td height="50">
								<label for="labelyear"><font size="5"><strong>ปี พ.ศ.ของหลักสูตร</strong></font></label>
								<label><font size="5" color="#FF0000">*</font></label>
							</td>
							<td height="50">
								<input name="year" type="text" style="font-size:18px" required maxlength="4" value="<%=YearOLD%>" />
							</td>
							<input type="hidden" id="Year" name="Year" value="<%=YearOLD%>" /> 
						</tr>
						
						<tr style="font-size:24px">
							<td width="500" height="50">
								<label for="labelnameDegreeTH"><font size="5"><strong>ชื่อปริญญา (ภาษาไทย)</strong></font></label>
								<label><font size="5" color="#FF0000">*</font></label>
							</td>
							<td width="500" height="50">
								<input name="nameDegreeTH" type="text" style="font-size:18px" required value="<%=BachelorNameTHOLD%>" />
								
							</td>
							<input type="hidden" id="BachelorNameTH" name="BachelorNameTH" value="<%=BachelorNameTHOLD%>" /> 
						</tr>
						
						<tr style="font-size:24px">
							<td width="500" height="50">
								<label for="labelnameDegreeEN"><font size="5"><strong>ชื่อปริญญา (ภาษาอังกฤษ)</strong></font></label>
								<label><font size="5" color="#FF0000">*</font></label>
							</td>
							<td width="500" height="50">
								<input name="nameDegreeEN" type="text" style="font-size:18px" required value="<%=BachelorNameENOLD%>" />
							</td>
							<input type="hidden" id="BachelorNameEN" name="BachelorNameEN" value="<%=BachelorNameENOLD%>" /> 
						</tr>
						
							
						<tr style="font-size:24px">
							<td width="500" height="50">
								<label for="labelmodified"><font size="5"><strong>ปีที่ปรับปรุงหลักสูตร(modified)</strong></font></label>
								<label><font size="5" color="#FF0000">*</font></label>
							</td>
							<td width="500" height="50">
							<%
									System.out.println("ModifiedOLD   "+ModifiedOLD);
									
									if(ModifiedOLD == null || ModifiedOLD.equals("null") || ModifiedOLD.equals("-")){%>
										<input name="modified" type="text" style="font-size:18px" value="-" />
									<%}else{%>														
									
										<input name="modified" type="text" style="font-size:18px" value="<%=ModifiedOLD%>" />										
									
							<%}%>	
							</td>
							<input type="hidden" id="Modified" name="Modified" value="<%=ModifiedOLD%>" /> 
						</tr>
						
						<tr style="font-size:24px">
							<td width="500" height="50">
								<label for="labeldepartment"><font size="5"><strong>ภาควิชา</strong></font></label>
								<label><font size="5" color="#FF0000">*</font></label>
							</td>
							<td>
							
						        
								<input type="hidden" name="departmentName" id="departmentName" value="<%=departmentOld%>" />
									<select name="department" id="department" required>
										<option value="<%=DepartmentIdOLD%>"><%=departmentOld%></option>
<%										
										for(Department dept : department) {
											if(DepartmentIdOLD == dept.getDepartmentId()) {
											}
											else{
										%>
												<option value="<%=dept.getDepartmentId()%>" id="<%=dept.getDepartmentId()%>"><%=dept.getDepartmentNameTH()%></option>	
										<%
											System.out.println(dept.getDepartmentId()+" AND  "+ dept.getDepartmentNameTH());
											}
										}
										%>
									</select>
							</td>
							<input type="hidden" name="departmentId" id="departmentId" value="<%=DepartmentIdOLD %>" />
						</tr>

						<tr style="font-size:24px">
							<td height="50">
								<label for="labelcredit"><font size="5"><strong>จำนวนหน่วยกิต</strong></font></label>
								<label><font size="5" color="#FF0000">*</font></label>
							</td>
							<td height="50">
								<input name="credit" type="text" required maxlength="3" style="font-size:18px" value="<%=CreditOLD%>" />
							</td>
							<input type="hidden" id="Credit" name="Credit" value="<%=CreditOLD%>" /> 
						</tr>
						
						<tr style="font-size:24px">
							<td height="50">
								<label for="labelCoorganizer"><font size="5"><strong>หลักสูตรความร่วมมือ</strong></font></label>
							</td>
							<td height="50">
								<input id="Coorganizer" name="Coorganizer" type="text" style="font-size:18px" value="<%=CoorganizerOLD%>" />
							</td>
							<input type="hidden" id="Coorganizer" name="Coorganizer" value="<%=CoorganizerOLD%>" /> 
						</tr>
						
						<tr style="font-size:24px">
							<td height="50">
								<label for="labelDegree"><font size="5"><strong>ระดับปริญญา</strong></font></label>
								<label><font size="5" color="#FF0000">*</font></label>
							</td>
							<td height="50">				
						<select id="Degree" name="Degree" required style="font-size:18px" value="<%=DegreeOLD%>">
  								<option value="">Select</option>
  								<option value="1">ปริญญาตรี</option>
								<option value="2">ปริญญาโท</option>
								<option value="3">ปริญญาเอก</option>								
						</select>
							</td>
							<input type="hidden" id="Degree" name="Degree" value="<%=DegreeOLD%>" /> 
						</tr>
						
						<tr style="font-size:24px">
							<td height="50">
								<label for="labelProgram"><font size="5"><strong>ประเภทของหลักสูตร</strong></font></label>
								<label><font size="5" color="#FF0000">*</font></label>
							</td>
							<td height="50">
						<select id="Program" name="Program" style="font-size:18px " required value="<%=ProgramOLD%>" >
								<option value="">Select</option>
								<option value="1">ปกติ</option>
								<option value="2">นานาชาติ</option>								
						</select>
							</td>
							<input type="hidden" id="Program" name="Program" value="<%=ProgramOLD%>" /> 
						</tr>
									
						<tr style="font-size:24px">
							<td height="50">
								<label for="labelOHCE"><font size="5"><strong>วันที่สำนักงานคณะกรรมการอุดมศึกษารับรอง</strong></font></label>
							</td>
							<td height="50">
								<input name="OHCE" type="date" style="font-size:18px " value="<%=OHCEOLD%>" />
							 
							</td>
							<input type="hidden" id="OHCE" name="OHCE" value="<%=OHCEOLD%>" /> 
						</tr>
						
						<tr style="font-size:24px">
							<td height="50">
								<label for="labelONEC"><font size="5"><strong>วันที่สภามหาวิทยาลัยมหิดลรับรอง</strong></font></label>
							</td>
							<td height="50">
								<input name="ONEC" type="date" style="font-size:18px " value="<%=ONECOLD%>" />
							  
							</td>
							<input type="hidden" id="ONEC" name="ONEC" value="<%=ONECOLD%>" /> 
						</tr>

						<tr style="font-size:24px">
							<td height="50">
								<label for="labelCurriculumNameTH"><font size="5"><strong>วันที่สภาวิศวกรรับรอง</strong></font></label>
							</td>
							<td height="50">
								<input id="EngApp" name="EngApp" type="date" style="font-size:18px" value="<%=EngAppOLD%>" />
							</td>
							<input type="hidden" id="EngApp" name="EngApp" value="<%=EngAppOLD%>" /> 
						</tr>

						<tr style="font-size:24px">
							<td height="50">
								<font size="3" color="#FF0000">หมายเหตุ: * คือ ข้อมูลสำคัญ จำเป็นต้องกรอก</font>
							</td>
							<td height="50">
							</td>
						</tr>

						<tr style="font-size:24px">
							<td height="50">							
								<center><input type="submit" value="save" name="save" class="buttonStyle"/>	</center>
							</td>
						</form>
							<td height="50">
								
								<center><a href="curriculum_edit_home.jsp"><button class="buttonStyle">	cancel</button></a></center>		
							</td>
						</tr>												
					</table>
				
					<div style="clear: both;"></div>
						<div id="footer"></div>
			</div>	
					<div style="clear: both;"></div>	
		</div>
    </div>
    
<%
		String checkButton = request.getParameter("save");


		// check save button is pressed
		if("save".equals(checkButton)) {	
			
				request.setCharacterEncoding("UTF-8");
				//String for store
				String  tempOHCE = null;
				String  tempONEC = null;
				String  tempEngApp = null;
				String  checkEngapp = null;
				
				// get value from tag
				String CurriculumNameTHedit = new String(request.getParameter("nameCurriculumTH").getBytes("ISO8859_1"),"UTF-8");
				String CurriculumNameENedit = request.getParameter("nameCurriculumEN");
				String BachelorNameTHedit = new String(request.getParameter("nameDegreeTH").getBytes("ISO8859_1"),"UTF-8");
				String BachelorNameENedit = request.getParameter("nameDegreeEN");
				String Modified = request.getParameter("modified");
				String Creditedit = request.getParameter("credit");				
				int DepartmentIdedit = Integer.parseInt((request.getParameter("department")));
				System.out.println("ID NEW:     "+ DepartmentIdedit);
				String Yearedit = request.getParameter("year");
				String Coorganizeredit = request.getParameter("Coorganizer");
				
				String Degreeedit = null;				
				int degree = Integer.parseInt(request.getParameter("Degree"));
				if(degree == 1){Degreeedit = "ปริญญาตรี";}
				else if(degree == 2){Degreeedit = "ปริญญาโท";}
				else if(degree == 3){Degreeedit = "ปริญญาเอก";}
				
				String Programedit = request.getParameter("Program");
				int program = Integer.parseInt(request.getParameter("Program"));
				if(program == 1){Programedit = "ปกติ";}
				else if(program == 2){Programedit = "นานาชาติ";}
				
				String OHCEedit = request.getParameter("OHCE");
				String ONECedit = request.getParameter("ONEC");				
				String EngAppedit = request.getParameter("EngApp");
				
//////////////////////////////CHECK IF INPUT WERE SPECIAL CHARACTERS> //////////////////////
	Pattern regex = Pattern.compile("[$&+,:;=?@#^*|!%]");
	Matcher matcherCurriculumNameTH = regex.matcher(CurriculumNameTHedit);
	Matcher matcherCurriculumNameEN = regex.matcher(CurriculumNameENedit);
	Matcher matcherBachelorNameTH = regex.matcher(BachelorNameTHedit);
	Matcher matcherBachelorNameEN = regex.matcher(BachelorNameENedit);
	Matcher matcherModified = regex.matcher(Modified);
	Matcher matcherCredit = regex.matcher(Creditedit);
	Matcher matcherYear = regex.matcher(Yearedit);
	Matcher matcherCoorganizer = regex.matcher(Coorganizeredit);
	
	
		
	if (matcherCurriculumNameTH.find() || matcherCurriculumNameEN.find() || matcherBachelorNameTH.find() || matcherModified.find() || matcherBachelorNameEN.find() || matcherCredit.find() || matcherYear.find() || matcherCoorganizer.find()){
		
		%>
						<script language="javascript"> alert("ข้อมูลไม่ถูกต้อง โปรดใช้ตัวเลขหรือตัวอักษรเท่านั้น");	</script>
		<%}else{
		
				
				//if input was null				
				if(OHCEedit.equals("")){
					tempOHCE = "null";
				}else{
					tempOHCE = "'" + request.getParameter("OHCE") + "'";
				}
				
				//if input was null
				if(ONECedit.equals("")){
					tempONEC = "null";
				}else{
					tempONEC = "'" + request.getParameter("ONEC") + "'";
				}
				
				//if input was null
				if(EngAppedit.equals("")){
					tempEngApp = "null";
				}else{
					tempEngApp = "'" + request.getParameter("EngApp") + "'";
				}					
%>
<%					if(Yearedit.length() != 4 || !Yearedit.matches("[-+]?\\d+(\\.\\d+)?")){// If year is not number%>
					<script>alert("กรุณาใส่ปีพ.ศ.ให้ถูกต้อง");</script>
<%					}
					else if(!Creditedit.matches("[-+]?\\d+(\\.\\d+)?")){//if Credit is not number%>
					<script>alert("กรุณาใส่จำนวนหน่วยกิตให้ถูกต้อง");</script>
<%					}
					else if(DepartmentIdedit==0){%>						
					<script>alert("ภาควิชา is required");</script>
<%					}
					else if (degree == 0){%>
					<script>alert("ระดับปริญญา is required");</script>
<%					}
					else if(program == 0){%>
					<script>alert("ประเภทของหลักสูตร is required");</script>
<%					}
					else{
	
							try {			   
									connect.createStatement().executeUpdate("UPDATE `curriculum` SET `curriculum_name_th`='"+ CurriculumNameTHedit +"',`curriculum_name_en`='"+ CurriculumNameENedit +"',`bachelor_name_th`='"+ BachelorNameTHedit +"',`bachelor_name_en`='"+ BachelorNameENedit +"',`modified`='"+ Modified +"',`department_id`='"
											+ DepartmentIdedit +"',`credit`='"+ Creditedit +"',`year`='"+ Yearedit +"',`co_organizer`='"+ Coorganizeredit +"',`degree`='"+ Degreeedit +"',`program`='"+ Programedit +"',`ohce_approved`="+ tempOHCE +",`onec_approved`="+ tempONEC +",`curriculum_EngApp`="+ tempEngApp +"" 
											+ " WHERE curriculum_id="+CurriculumId); 
									System.out.println("UPDATE 1");/////////////////////////////
									
									String sql_curriculum = "UPDATE `curriculum` SET `curriculum_name_th`='"+ CurriculumNameTHedit +"',`curriculum_name_en`='"+ CurriculumNameENedit +"',`bachelor_name_th`='"+ BachelorNameTHedit +"',`bachelor_name_en`='"+ BachelorNameENedit +"',`modified`='"+ Modified +"',`department_id`='"
											+ DepartmentIdedit +"',`credit`='"+ Creditedit +"',`year`='"+ Yearedit +"',`co_organizer`='"+ Coorganizeredit +"',`degree`='"+ Degreeedit +"',`program`='"+ Programedit +"',`ohce_approved`="+ tempOHCE +",`onec_approved`="+ tempONEC +",`curriculum_EngApp`="+ tempEngApp +"" 
											+ " WHERE curriculum_id="+CurriculumId;
									
									/* Log file */
									String log_user = strUser+" edit curriculum";
									Log.writeUserFile(log_user);
									Log.writeUserFile(sql_curriculum);		
									
									
									connect.createStatement().executeUpdate("UPDATE `course` SET `ohce_approved`="+tempOHCE+", `onec_approved`="+tempONEC+""
											+ " WHERE curriculum_id ="+CurriculumId);
									System.out.println("UPDATE 2");/////////////////////////////
									
									
									String sql_course = "UPDATE `course` SET `ohce_approved`="+tempOHCE+", `onec_approved`="+tempONEC+""
											+ " WHERE curriculum_id ="+CurriculumId;
									
									/* Log file */
									String log_user2 = strUser+" edit course";
									Log.writeUserFile(log_user2);
									Log.writeUserFile(sql_course);						
									
									
			
									/*pstmt.executeUpdate();  
									ResultSet keys = pstmt.getGeneratedKeys();    
									keys.next();  
									int key = keys.getInt(1);*/
									String CurriculumId_cat = CurriculumId;
									String Credit_cat = Creditedit;
									String OHCEDATE = tempOHCE;
									String ONECDATE = tempONEC;
									String EngAppDATE = tempEngApp;
									// Storage CurId in session
									session.setAttribute("CId", CurriculumId_cat);
									session.setAttribute("CCredit", Credit_cat);
									session.setAttribute("OHCE", OHCEDATE);
									session.setAttribute("ONEC", ONECDATE);
									session.setAttribute("EngApp", EngAppDATE);
									session.setAttribute("checker", "FALSE");
									session.setAttribute("value", 0);
									
									System.out.println("Phase 1 Success with CurriID :"+ CurriculumId_cat);
									System.out.println("Phase 2 Success with CurriCredit :"+ Creditedit);
									System.out.println("Phase 3 Success with OHCEDATE :"+ OHCEDATE);
									System.out.println("Phase 4 Success with ONECDATE :"+ ONECDATE);
									System.out.println("Phase 5 Success with EngApp :"+ EngAppDATE);						
			
			%>
										<script language="javascript"> alert("บันทึกข้อมูลสำเร็จ");	</script>							
									<<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=curriculum_category_edit.jsp"> 
										
			<%
								} catch (SQLException e) {
									e.printStackTrace();
								}
			}
		}///////else special char
		
		}
		
		connect.close();
%>
    
	<!-- 	<script type="text/javascript" src="js/curriculum_add_new1_validation.js"></script> -->


</body>
</html>